package cn.wangqi.personal.dao;

import cn.wangqi.personal.domain.BlogArticle;
import cn.wangqi.personal.util.Page;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *created by admin  2017-10-17 02:38:27
 */
@Repository
public class BlogArticleDao{
    private final String NAMESPACE = this.getClass().getName() + ".";
    private Logger logger = Logger.getLogger(BlogArticleDao.class);
    @Resource
    protected SqlSessionTemplate mainSqlSession;

    public List<BlogArticle> listBlogArticle(Integer userId,Page page){
        Map<String, Object> params = new HashMap<>();
        params.put("page",page);
        params.put("userId",userId);
        return mainSqlSession.selectList(NAMESPACE + "listBlogArticle", params);
    }
    public List<BlogArticle> findRecomArticleByUserId(Integer userId){
        Map<String, Object> params = new HashMap<>();
        params.put("userId",userId);
        return mainSqlSession.selectList(NAMESPACE + "findRecomArticleByUserId", params);
    }




    public void saveBlogArticle(BlogArticle blogArticle) {
        mainSqlSession.insert(NAMESPACE + "saveBlogArticle", blogArticle);
    }

    public void saveBlogArticle(List<BlogArticle> blogArticle) {
        mainSqlSession.insert(NAMESPACE + "saveBlogArticleList", blogArticle);
    }

    public void deleteBlogArticleById(int blogArticleId) {
        mainSqlSession.delete(NAMESPACE + "deleteBlogArticleById", blogArticleId);
    }

    public BlogArticle findBlogArticleById(int blogArticleId){
        return mainSqlSession.selectOne(NAMESPACE + "findBlogArticleById", blogArticleId);
    }
    public int countBlogArticle(){
        Map<String, Object> params = new HashMap<>();
        return mainSqlSession.selectOne(NAMESPACE + "totalBlogArticle",params);
    }

    public void updateBlogArticle(BlogArticle blogArticle){
        mainSqlSession.update(NAMESPACE + "updateBlogArticle", blogArticle);
    }

}
